<?php
namespace app\payclass\model;

use app\BaseModel;
use app\member\model\Level;
use app\member\model\Member;
use think\facade\Db;

class ClassList extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_payclass_list';

    protected $append = [

        'cate_name',

        'spv'
    ];


    /**
     * @author chenniang
     * @DataTime: 2020-12-16 14:22
     * @功能说明:总的浏览量
     */
    public function getSpvAttr($value,$data){

        if(isset($data['pv'])&&isset($data['true_pv'])){

            return $data['pv']+$data['true_pv'];
        }

    }


    /**
     * @param $value
     * @param $data
     * @功能说明:分类名字
     * @author chenniang
     * @DataTime: 2020-12-16 14:22
     */

    public function getCateNameAttr($value,$data){

        if(!empty($data['cate_id'])){

            $cate_model = new Cate();

            return $cate_model->where(['id'=>$data['cate_id']])->value('title');
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-09 11:20
     * @功能说明:列表
     */

    public function topClassList($dis){

        $data = $this->where($dis)->field('*,(pv+true_pv) as spv')->order('spv desc,top desc,id desc')->limit(10)->select()->toArray();

        return $data;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-08 10:51
     * @功能说明:列表
     */
    public function classList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-09 17:05
     * @功能说明:配置详情
     */
    public function classInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)? $data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:配置添加
     */
    public function classAdd($data){

        $data['create_time'] = time();

        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:配置编辑
     */
    public function classUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-14 15:15
     * @功能说明:查看该用户是否有该课程都观看权限
     */
    public function lookAuthOld($class,$user_id){

        $auth_model = new Auth();

        $member_model = new Level();

        $dis = [
            'user_id' => $user_id,

            'class_id'=> $class['id']
        ];

        $info = $auth_model->where($dis)->field('id')->find();

        $auth = 0;
        //全部免费
        if($class['watch_type']==0){

            $auth = 1;
        //全部付费
        }elseif ($class['watch_type']==1){

            if(!empty($info)){

                $auth = 1;
            }
        //只有会员可看
        }elseif ($class['watch_type']==2){

            $member = $member_model->getUserLevel($user_id,$class['uniacid']);

            if(!empty($member)){

                $auth = 1;

            }
        //会员免费 用户付费
        }elseif ($class['watch_type']==3){

            $member = $member_model->getUserLevel($user_id,$class['uniacid']);
            //会员
            if(!empty($member)){

                $auth = 1;

            }
            //付费
            if (!empty($info)){

                $auth = 1;

            }

        }

        return $auth;

    }


    /**
     * @param $class
     * @param $user_id
     * @功能说明:
     * @author chenniang
     * @DataTime: 2020-12-16 17:37
     */
    public function lookAuth($class,$user_id){

        $auth_model = new Auth();

        $dis = [

            'user_id' => $user_id,

            'class_id'=> $class['id']
        ];

        $info = $auth_model->where($dis)->field('id')->find();

        $auth = !empty($info)?1:0;

        return $auth;

    }


    /**
     * @param $class
     * @param $user_id
     * @功能说明:可以领取 主要判断是否弹出可以领取的按钮
     * @author chenniang
     * @DataTime: 2020-12-17 17:17
     */

    public function canAddAuth($class,$user_id){

        $auth_model   = new Auth();

        $member_model = new Level();

        $record_model = new Record();

        $dis = [
            'user_id' => $user_id,

            'class_id'=> $class['id']
        ];

        $info = $auth_model->where($dis)->field('id')->find();

        $auth = 0;
        //全部免费
        if($class['watch_type']==0){

            $auth = 1;
            //全部付费
        }elseif ($class['watch_type']==1){

//            if(!empty($info)){
//
//                $auth = 1;
//            }
            //只有会员可看
        }elseif ($class['watch_type']==2){

            $member = $member_model->getUserLevel($user_id,$class['uniacid']);

            if(!empty($member)){

                $auth = 1;

            }
            //会员免费 用户付费
        }elseif ($class['watch_type']==3){

            $member = $member_model->getUserLevel($user_id,$class['uniacid']);
            //会员
            if(!empty($member)){

                $auth = 1;

            }

        }
        //参加了活动的
        $atv = $record_model->recordInfo(['class_id'=>$class['id'],'user_id'=>$user_id,'status'=>3]);

        if(!empty($atv)){

            $auth = 1;

        }
        //如果领取过就不需要再领取了
        if(!empty($info)){

            $auth = 0;

        }

        return $auth;

    }










}